package com.easylinkin.linkappapi.common.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * @Description 第三方组件的引入管理
 * @ClassName MessageConfig
 * @Author 徐国庆
 * @date 2020.05.15 13:57
 */
@Configuration
public class MessageConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageConfig.class);


    /**
     * 手机短信的配置
     */
    @Value("${message.product}")
    private String product;
    @Value("${message.domain}")
    private String domain;
    @Value("${message.accessKeyId}")
    private String accessKeyId;
    @Value("${message.accessKeySecret}")
    private String accessKeySecret;
    @Value("${message.regingId}")
    private String regingId;

    /**
     * 邮件的配置
     */
    @Value("${message.host}")
    private String host;
    @Value("${message.username}")
    private String username;
    @Value("${message.password}")
    private String password;
    @Value("${message.subject}")
    private String subject;
    @Value("${message.port}")
    private Integer port;




    @Bean
    public  JavaMailSenderImpl   javaMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(host);
        sender.setUsername(username);
        sender.setPassword(password);
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");//开启认证
        properties.setProperty("mail.smtp.connectiontimeout", "10000");//设置链接超时
        properties.setProperty("mail.smtp.timeout", "10000");//
        properties.setProperty("mail.smtp.writetimeout", "10000");//\
        properties.setProperty("mail.smtp.port", Integer.toString(port));//设置端口
        properties.setProperty("mail.smtp.socketFactory.port", Integer.toString(port));//设置ssl端口
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        sender.setJavaMailProperties(properties);
        return sender;
    }
    /**
     * MimeMessageHelper 的引入管理(多例)
     */
    @Bean
    @Scope("prototype")
    public MimeMessageHelper mimeMessageHelper() {
        JavaMailSenderImpl sender = javaMailSender();
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8"); //为了解决中文乱码问题
        //设置发件人
        try {
            helper.setFrom(username);
            //设置邮件主题
            helper.setSubject(subject);
        } catch (MessagingException e) {
            LOGGER.error(" set  sendEmailInfo    is wrong!", e);
        }
        return helper;
    }

    /**
     * IAcsClient 的引入管理
     */
    @Bean
    public IAcsClient acsClient() {
        IAcsClient acsClient = null;
        IClientProfile profile = DefaultProfile.getProfile(regingId, accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint(regingId, regingId, product, domain);
            acsClient = new DefaultAcsClient(profile);
        } catch (ClientException e) {
            LOGGER.error(" get  IAcsClient is wrong!", e);
        }
        return acsClient;
    }


}

